layui.use(['form', 'jquery', 'layer', 'table', 'laydate','upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var upload = layui.upload;
	// 获取品牌列表
	var get_brand_list = function(category_sign,brand_name,is_select){
		var post_data = {
			page:1,
			limit:1000,
			_o:'brand_id',
			_b:'desc',
			brand_name:brand_name,
			category_sign:category_sign,
			is_select:is_select
		};
		send_info(get_brand_uri,post_data,function(data){
			if(is_select == 'no'){
				$('#select_brand'+category_sign).find('.box').html('');
			} else {
				$('#selected_brand'+category_sign).find('.box').html('');
			}
			for(var i in data.data){
				var e = data.data[i];
				if(is_select == 'no'){
					$('#select_brand'+category_sign).find('.box').append('<input type="checkbox" name="brand_sign[]" lay-filter="category_brand" value="'+e.brand_sign+'" title="'+e.brand_name+'">');
				} else {
					$('#selected_brand'+category_sign).find('.box').append('<input type="checkbox" name="brand_sign[]" lay-filter="category_brand_select" value="'+e.brand_sign+'" title="'+e.brand_name+'" checked>');
				}
			}
			form.render('checkbox');
		})
	}
	// 监听分类 品牌按钮
	form.on('checkbox(category_brand)',function(){
		var post_data = {
			category_sign:category_sign,
			brand_sign:$(this).val()
		};
		send_info(save_category_brand_uri,post_data,function(data){
			layer.msg('添加成功',{icon:1});
			get_brand_list(category_sign,$('#select_brand'+category_sign+' input[type="search"]').val(),'no');
			get_brand_list(category_sign,'','yes');
		})
	})
	form.on('checkbox(category_brand_select)',function(){
		var post_data = {
			category_sign:category_sign,
			brand_sign:$(this).val()
		};
		send_info(del_category_brand_uri,post_data,function(data){
			layer.msg('移除成功',{icon:1});
			get_brand_list(category_sign,'','no');
			get_brand_list(category_sign,$('#selected_brand'+category_sign+' input[type="search"]').val(),'yes');
		})
	})
	// 搜索
	var brand_name = '';
	$(document).on('input propertychange','#select_brand'+category_sign+' input[type="search"]',function(){
		if($(this).val() != brand_name){
			brand_name = $(this).val();
			get_brand_list(category_sign,brand_name,'no');
		}
	})
	var brand_name_selected= '';
	$(document).on('input propertychange','#selected_brand'+category_sign+' input[type="search"]',function(){
		brand_name_selected = $(this).val();
		get_brand_list(category_sign,brand_name_selected,'yes');
	})
	get_brand_list(category_sign,'','no');
	get_brand_list(category_sign,'','yes');
	// 上传
	upload.render({
		elem:'#category_picture_upload'+category_sign,
		url:upload_uri,
		accept:'images',
		acceptMime:'image/*',
		auto:true,
		field:'category_picture_upload'+category_sign,
		multiple:false,
		drag:true,
		before:function(obj){
			layer.load();
		},
		done:function(res,index,upload){
			if(res.code == 0){
				var item = this.item
				if(item == undefined){
					item = $('.layui-upload-drag');
				}
				var html = createUploadHtml('category_picture',res.data);
				item.prev('.layui-upload-list').html(html);
				layer.msg('上传成功',{icon:1});
			} else {
				layer.msg(e.msg,{icon:2});
			}
			layer.closeAll('loading'); 
		},
		error:function(index,upload){
			layer.msg('网络错误',{icon:2});
			layer.closeAll('loading');
		}
	});
	
	var lay_filter = 'tpl_category' + category_sign;
	form.val(lay_filter, category_form_value);
	// 表单渲染
	form.render('checkbox');
	form.render('select');
	// 初始化图片
	if(category_form_value.category_picture_info){
		var html = createUploadHtml('category_picture',category_form_value.category_picture_info);
		$('#layui-upload-list'+category_sign).html(html);
	}

	// 监听复选框
	form.on('checkbox(category_goods_alone)', function(data) {
		var check_name = $(this).attr('name');
		var value = data.elem.checked;
		if(value == true) {
			$('.goods-audit-div').hide();
		} else {
			$('.goods-audit-div').show();
		}
	})
	// 监听提交
	form.on('submit(submit_category)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="' + lay_filter + '"]').attr('action');
		if(field.category_name == '') {
			layer.msg('分类名称必须填写', {
				icon: 2,
				amim: 6
			})
			$('input[name="category_name"]').addClass('.layui-form-danger').focus();
			return false;
		}
		send_info(url, field, function(data) {
			if(data.code == 1) {
				layer.msg('保存成功', {
					icon: 1
				});
				// 关闭当前页 刷新父页
				$('.layui-body .layui-tab[lay-filter="content"] li.layui-this .layui-tab-close').trigger('click');
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="category"]').trigger('click');
				// 父页面点击
				$('.layui-body .layui-tab[lay-filter="content"] li[lay-id="category_list"]').trigger('click');
				// 表格重载
				if(data.data['parent_sign'] == '0'){
					table.reload('category_list');
				} else if(data.data['top_id'] == '0'){
					var id = 'category_list_son'+data.data['parent_sign'];
					if($('#'+id).length >0){
						table.reload(id);
					} else {
						$(document).find('div[lay-id="category_list"]').find('td[data-val="'+data.data['parent_sign']+'"][data-field="category_sign"]').prev('td').prev('td').find('div span').trigger('click');
					}
				} else {
					var id = 'category_list_end'+data.data['parent_sign'];
					if($('#'+id).length >0){
						table.reload(id);
					} else {
						$(document).find('div[lay-id="category_list"]').find('td[data-val="'+data.data['parent_sign']+'"][data-field="category_sign"]').prev('td').prev('td').find('div span').trigger('click');
					}
				}
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})

})